<template>
  <div class="container">
    <div class="login-box">
      <h2>{{ $t("Login.userLogin") }}</h2>
      <LangToggle class="icon"></LangToggle>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="form">
        <el-form-item prop="username">
          <BaseInput
            v-model="ruleForm.username"
            clearable
            :placeholder="$t('Login.inputUsername')"
            size="large"
            width="100%"
          ></BaseInput>
        </el-form-item>
        <el-form-item>
          <BaseInput
            v-model="ruleForm.password"
            showPassword
            :placeholder="$t('Login.inputPassword')"
            size="large"
            width="100%"
          ></BaseInput>
        </el-form-item>
      </el-form>
      <BaseButton
        :content="$t('Login.login')"
        @click="login(ruleFormRef)"
        class="login-btn"
        size="large"
      ></BaseButton>
    </div>
  </div>
</template>

<script setup lang="ts">
// 获取当前环境
// console.log(import.meta.env);
import { reactive, ref, onMounted } from "vue";
import type { FormInstance } from "element-plus";
// import axios from 'axios'
// import { Request } from "@/utils/request";
// 路由跳转
import router from "@/router";
// 引入api
import Speed from "@/api/work/speed";
// ts中使用i18n
import i18n from "@/locales";
// i18n Error: 类型实例化过深，且可能无限。
const { t }: any = i18n.global;

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "admin",
  password: "",
});
// 自定义校验规则
const CheckUserName = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error(t("Login.inputUsername")));
    return;
  }
  callback();
};
const rules = reactive({
  username: [{ validator: CheckUserName, trigger: "blur" }],
});
// 登录操作
const login = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      // console.log(ruleForm);
      // 路由跳转是异步操作，所以需要await
      // console.log(router,'router');
      await router.push("/charts");
    }
  });
};

// axios.get('/api/getUsers').then((res)=>{
//   console.log(res);
// })
// const MockRequest = new Request({
//   // baseURL:""
// })
// MockRequest.get('/api/getUsers').then((res)=>{
//   console.log(res);
// })
//

// onMounted(async () => {
//   const res = await Speed.Get_Status_List();
//   console.log(res);
// })
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: url("@/assets/imgs/common/login-bg.webp");
  .login-box {
    position: relative;
    @include bg_color("bg_color_1");
    height: 240px;
    width: 400px;
    padding: 30px;
    border-radius: 10px;
    @include font_color("font_color");
    h2 {
      text-align: center;
      padding-top: 20px;
      // color: $black_3b3b3b;
    }
    .icon {
      position: absolute;
      top: 18px;
      right: 24px;
    }
    .form {
      margin-top: 40px;
      // :deep(.input) {
      //   width: 400px;
      // }
    }
    .login-btn {
      width: 400px;
    }
  }
}
</style>
